<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="任务代码" prop="courseCode">
        <el-input v-model="dataForm.courseCode" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="任务名称" prop="courseName">
        <el-input v-model="dataForm.courseName" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="教师姓名" prop="courseTeacher">
        <el-input v-model="dataForm.courseTeacher" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="上课地点" prop="courseAddress">
        <el-input v-model="dataForm.courseAddress" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="上课时间" prop="courseDate">
        <el-input v-model="dataForm.courseDate" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="任务学时" prop="courseTime">
        <el-input v-model="dataForm.courseTime" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="总容量" prop="coursePersions">
        <el-input v-model="dataForm.coursePersions" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="任务性质" prop="courseXingzhi">
        <el-input v-model="dataForm.courseXingzhi" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="开课学院" prop="courseHome">
        <el-input v-model="dataForm.courseHome" placeholder=""></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          moduleId: '',
          createTime: '',
          courseCode: '',
          courseName: '',
          courseTeacher: '',
          courseAddress: '',
          courseDate: '',
          courseCred: '',
          courseTime: '',
          coursePersions: '',
          courseLess: '',
          courseXingzhi: '',
          courseHome: ''},
        dataRule: {
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id, module) {
        this.dataForm.moduleId = module
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/sys/jmcourse/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.jmcourse
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.$http({
                url: `/sys/jmcourse/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      }
    }
  }
</script>
